<template>
  <div >
    <x-header :left-options="{showBack: true}" >添加课程</x-header>
    <group>
      <selector title="课程" :options="courselist" v-model="defaultcourse"></selector>
    </group>
    <br>
    <x-button type="warn" text="添加课程" @click.native="showConfirm"></x-button>
    <confirm v-model="show" >
        
    </confirm>
  </div>
</template>
<script>
  import {XHeader, Actionsheet,Group ,Selector,XButton ,Confirm } from 'vux';
  import config from '../config.js';


  export default {
    
    data () {
      return {
        show:false,
        courselist:[],
        defaultcourse:'',
      }
     },
     mounted() {
        this.getCourse()
      },
     methods:{
      getCourse(){
        this.$vux.loading.show({
         text: '加载中'
        })
        this.$http({
          url:config.base+"/course/list?pageNum=1&&pageSize=100",
          method:"GET",
          headers:{
            'content-type':'application/json',
          },
        }).then( rep => {
          let data=rep.data.list;
          for(let i=0;i<data.length;i++){
            this.courselist.push({key:data[i].id,value:data[i].name})
          }
          this.$vux.loading.hide()
        }).catch( (error) => {
          this.$vux.loading.hide()
          this.$vux.toast.show({
            text:"网络错误",
            type:'warn'
          })
        })
      },
      showConfirm(){
        const courseId = this.defaultcourse;
        courseId === ""?
        this.$vux.toast.show({
          text:'请选择报名课程',
          type:'warn'
        })
        :
        this.$vux.confirm.show({
          title: '提示',
          content: '确认添加课程吗？',
          onConfirm :()=>{
            this.$http({
              url:config.base+"apply/submit",
              method:"POST",
              headers:{
                'content-type':'application/x-www-form-urlencoded; charset=UTF-8',
              },
              data:this.qs.stringify({courseId, idCardNum:localStorage.idCardNum})
            }).then( rep => {
              let data=rep.data
              if(data.status === 200){
                this.$vux.toast.show({
                  text:data.message,
                  time:1000
                })
              }else{
                this.$vux.toast.show({
                  text:data.message,
                  time:1000
                })
              }
              this.$vux.loading.hide()
            }).catch( (error) => {
              this.$vux.loading.hide()
              this.$vux.toast.show({
                text:"网络错误",
                type:'warn'
              })
            })
          }
        })
      },
     },
     components: {
      XHeader,
      Actionsheet,
      Group,
      XButton,
      Selector,
      Confirm
    },
  }
</script>
<style>
</style>